package com.sgu6.sgu6_machine.ui.fragment.set;


import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.sgu6.middleware.enums.MachineCmdEnum;
import com.sgu6.middleware.util.HexUtil;
import com.sgu6.serialport.manager.SerialPortManager;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.base.BaseF;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 电机设置
 */
public class MotorF extends BaseF {

    @BindView(R.id.etMillTime)
    EditText etMillTime;
    @BindView(R.id.etPullMillTime)
    EditText etPullMillTime;
    @BindView(R.id.etPullMoTime)
    EditText etPullMoTime;
    @BindView(R.id.etOverMoTime)
    EditText etOverMoTime;
    @BindView(R.id.etBlowChaffTime)
    EditText etBlowChaffTime;
    @BindView(R.id.etPushMilletTime)
    EditText etPushMilletTime;
    @BindView(R.id.etPushMoTime)
    EditText etPushMoTime;
    @BindView(R.id.etOpenDoorTime)
    EditText etOpenDoorTime;

    @BindView(R.id.btnStartPullMo)
    Button btnStartPullMo;
    @BindView(R.id.btnStopPullMo)
    Button btnStopPullMo;
    @BindView(R.id.btnStopPullMoAfterTime)
    Button btnStopPullMoAfterTime;
    @BindView(R.id.btnStartOverMo)
    Button btnStartOverMo;
    @BindView(R.id.btnStopOverMo)
    Button btnStopOverMo;
    @BindView(R.id.btnStopOverMoAfterTime)
    Button btnStopOverMoAfterTime;
    @BindView(R.id.btnStartPushMo)
    Button btnStartPushMo;
    @BindView(R.id.btnStopPushMo)
    Button btnStopPushMo;
    @BindView(R.id.btnStopPushMoAfterTime)
    Button btnStopPushMoAfterTime;
    @BindView(R.id.btnStartPushMillet)
    Button btnStartPushMillet;
    @BindView(R.id.btnStopPushMillet)
    Button btnStopPushMillet;
    @BindView(R.id.btnStopPushMilletAfterTime)
    Button btnStopPushMilletAfterTime;
    Unbinder unbinder;
    @BindView(R.id.btnStartHorT)
    Button btnStartHorT;
    @BindView(R.id.btnStopHorT)
    Button btnStopHorT;
    @BindView(R.id.btnStartVerT)
    Button btnStartVerT;
    @BindView(R.id.btnStopVerT)
    Button btnStopVerT;
    @BindView(R.id.btnStartOpenDoor)
    Button btnStartOpenDoor;
    @BindView(R.id.btnStopOpenDoor)
    Button btnStopOpenDoor;
    @BindView(R.id.btnStopOpenDoorAfterTime)
    Button btnStopOpenDoorAfterTime;


    public static MotorF newInstance() {
        return new MotorF();
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_motor, container, false);
        unbinder = ButterKnife.bind(this, v);

        if (!MachineContainer.machineConfigDto.withPack) {
            btnStartPullMo.setVisibility(View.GONE);
            btnStopPullMo.setVisibility(View.GONE);
            btnStopPullMoAfterTime.setVisibility(View.GONE);

            btnStartOverMo.setVisibility(View.GONE);
            btnStopOverMo.setVisibility(View.GONE);
            btnStopOverMoAfterTime.setVisibility(View.GONE);

            btnStartPushMo.setVisibility(View.GONE);
            btnStopPushMo.setVisibility(View.GONE);
            btnStopPushMoAfterTime.setVisibility(View.GONE);

            btnStartPushMillet.setVisibility(View.GONE);
            btnStopPushMillet.setVisibility(View.GONE);
            btnStopPushMilletAfterTime.setVisibility(View.GONE);


            etPullMoTime.setVisibility(View.GONE);
            etOverMoTime.setVisibility(View.GONE);
            etPushMoTime.setVisibility(View.GONE);
            etPushMilletTime.setVisibility(View.GONE);

            btnStartHorT.setVisibility(View.GONE);
            btnStopHorT.setVisibility(View.GONE);
            btnStartVerT.setVisibility(View.GONE);
            btnStopVerT.setVisibility(View.GONE);

        } else {
            btnStartOpenDoor.setVisibility(View.GONE);
            btnStopOpenDoor.setVisibility(View.GONE);
            btnStopOpenDoorAfterTime.setVisibility(View.GONE);
            btnStartPushMo.setVisibility(View.GONE);
            btnStopPushMo.setVisibility(View.GONE);
        }
        return v;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.btnStartOpenDoor, R.id.btnStopOpenDoor, R.id.btnStopOpenDoorAfterTime, R.id.btnStartMill, R.id.btnStopMill, R.id.btnStopMillAfterTime, R.id.btnStartPullMill, R.id.btnStopPullMill, R.id.btnStopPullMillAfterTime, R.id.btnStartPullMo, R.id.btnStopPullMo, R.id.btnStopPullMoAfterTime, R.id.btnStartOverMo, R.id.btnStopOverMo, R.id.btnStopOverMoAfterTime, R.id.btnStartBlowChaff, R.id.btnStopBlowChaff, R.id.btnStopBlowChaffAfterTime, R.id.btnStartPushMillet, R.id.btnStopPushMillet, R.id.btnStopPushMilletAfterTime, R.id.btnStartPushMo, R.id.btnStopPushMo, R.id.btnStopPushMoAfterTime, R.id.btnStopAccuracy, R.id.btnResetAccuracy, R.id.btnLeftAccuracy, R.id.btnRightAccuracy, R.id.btnStartHorT, R.id.btnStopHorT, R.id.btnStartVerT, R.id.btnStopVerT})
    public void onViewClicked(View view) {
        Log.i(TAG, "onViewClicked: ");
        switch (view.getId()) {
            case R.id.btnStartOpenDoor:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.开门电机开到位.code()));
                break;
            case R.id.btnStopOpenDoor:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.开门电机关到位.code()));
                break;
            case R.id.btnStopOpenDoorAfterTime:
//                String stopOpenDoorAfterTime = etOpenDoorTime.getText().toString().trim();
//                if (stopOpenDoorAfterTime.length() == 0) {
//                    ToastUtil.show(getActivity(), "开关门电机关闭时间不能为空");
//                    break;
//                }
//                String spm = String.format(MachineCmdEnum.启动并按时关闭开关.code(), Integer.parseInt(stopOpenDoorAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即停止开门电机.code()));

                break;
            case R.id.btnStartMill:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动吹糠电机.code()));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动碾米电机.code()));
                break;
            case R.id.btnStopMill:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭碾米电机.code()));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭吹糠电机.code()));
                break;
            case R.id.btnStopMillAfterTime:
                String stopMillAfterTime = etMillTime.getText().toString().trim();
                if (stopMillAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "碾米电机关闭时间不能为空");
                    break;
                }
                String sm = String.format(MachineCmdEnum.启动并按时关闭碾米电机.code(), Integer.parseInt(stopMillAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(sm));
                break;
            case R.id.btnStartPullMill:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动下米电机.code()));
                break;
            case R.id.btnStopPullMill:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭下米电机.code()));
                break;
            case R.id.btnStopPullMillAfterTime:
                String stopPullMillAfterTime = etPullMillTime.getText().toString().trim();
                if (stopPullMillAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "下米电机关闭时间不能为空");
                    break;
                }
                String spm = String.format(MachineCmdEnum.启动并按时关闭下米电机.code(), Integer.parseInt(stopPullMillAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(spm));
                break;
            case R.id.btnStartPullMo:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动拉膜电机.code()));
                break;
            case R.id.btnStopPullMo:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭拉膜电机.code()));
                break;
            case R.id.btnStopPullMoAfterTime:
                String stopPullMoAfterTime = etPullMoTime.getText().toString().trim();
                if (stopPullMoAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "拉膜电机关闭时间不能为空");
                    break;
                }
                String spma = String.format(MachineCmdEnum.启动并按时关闭拉膜电机.code(), Integer.parseInt(stopPullMoAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(spma));
                break;
            case R.id.btnStartOverMo:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动封膜电机.code()));
                break;
            case R.id.btnStopOverMo:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭封膜电机.code()));
                break;
            case R.id.btnStopOverMoAfterTime:
                String stopOverMoAfterTime = etOverMoTime.getText().toString().trim();
                if (stopOverMoAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "封膜电机关闭时间不能为空");
                    break;
                }
                String som = String.format(MachineCmdEnum.启动并按时关闭封膜电机.code(), Integer.parseInt(stopOverMoAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(som));
                break;
            case R.id.btnStartBlowChaff:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动吹糠电机.code()));
                break;
            case R.id.btnStopBlowChaff:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭吹糠电机.code()));
                break;
            case R.id.btnStopBlowChaffAfterTime:
                String stopBlowChaffAfterTime = etBlowChaffTime.getText().toString().trim();
                if (stopBlowChaffAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "吹糠电机关闭时间不能为空");
                    break;
                }
                String sbc = String.format(MachineCmdEnum.启动并按时关闭吹糠电机.code(), Integer.parseInt(stopBlowChaffAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(sbc));
                break;
            case R.id.btnStartPushMillet:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动送谷电机.code()));
                break;
            case R.id.btnStopPushMillet:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭送谷电机.code()));
                break;
            case R.id.btnStopPushMilletAfterTime:
                String stopPushMilletAfterTime = etBlowChaffTime.getText().toString().trim();
                if (stopPushMilletAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "送谷电机关闭时间不能为空");
                    break;
                }
                String spmat = String.format(MachineCmdEnum.启动并按时关闭送谷电机.code(), Integer.parseInt(stopPushMilletAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(spmat));
                break;
            case R.id.btnStartPushMo:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.启动送膜电机.code()));
                break;
            case R.id.btnStopPushMo:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.立即关闭送膜电机.code()));
                break;
            case R.id.btnStopPushMoAfterTime:
                String stopPushMoAfterTime = etPushMoTime.getText().toString().trim();
                if (stopPushMoAfterTime.length() == 0) {
                    ToastUtil.show(getActivity(), "送膜电机关闭时间不能为空");
                    break;
                }
                String stpm = String.format(MachineCmdEnum.启动并按时关闭送膜电机.code(), Integer.parseInt(stopPushMoAfterTime));
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(stpm));
                break;
            case R.id.btnStopAccuracy:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.停止精度电机.code()));
                break;
            case R.id.btnResetAccuracy:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.复位精度电机.code()));
                break;
            case R.id.btnLeftAccuracy:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.反转精度电机向左.code()));
                break;
            case R.id.btnRightAccuracy:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.正转精度电机向右.code()));
                break;
            case R.id.btnStartHorT:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.开始横封加热.code()));
                break;
            case R.id.btnStopHorT:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.关闭横封加热.code()));
                break;
            case R.id.btnStartVerT:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.开始纵封加热.code()));
                break;
            case R.id.btnStopVerT:
                SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.关闭纵封加热.code()));
                break;
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.i(TAG, "setUserVisibleHint: " + isVisibleToUser);

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Log.i(TAG, "onHiddenChanged: " + hidden);
        if (hidden) {
            SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.自动模式.code()));
        } else {
            SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.手动模式.code()));
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SerialPortManager.getInstance().sendBytes(HexUtil.HexString2Bytes(MachineCmdEnum.自动模式.code()));
    }
}
